<template>
  <div>
    <div class="main">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="160px"
        class="demo-ruleForm"
      >
        <el-form-item label="博客名称" prop="blogName" size="mini">
          <el-input v-model="ruleForm.blogName" clearable></el-input>
        </el-form-item>
        <el-form-item label="作者名" prop="author" size="mini">
          <el-input v-model="ruleForm.author" clearable></el-input>
        </el-form-item>
        <el-form-item label="作者头像">
          <el-upload
            class="avatar-uploader"
            action="/admin/file/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="ruleForm.avatar" :src="ruleForm.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="介绍语" prop="introduction" size="mini">
          <el-input type="textarea" v-model="ruleForm.introduction"></el-input>
        </el-form-item>
        <el-form-item label="开启GitHub访问" prop="GitHub">
          <el-switch v-model="GitHub"></el-switch>
        </el-form-item>
        <el-form-item label="GitHub 主页访问地址" prop="githubHome" size="mini" v-if="GitHub">
          <el-input v-model="ruleForm.githubHome" clearable></el-input>
        </el-form-item>
        <el-form-item label="开启CSDN访问" prop="Csdn">
          <el-switch v-model="Csdn"></el-switch>
        </el-form-item>
        <el-form-item label="CSDN 主页访问地址" prop="csdnHome" size="mini" v-if="Csdn">
          <el-input v-model="ruleForm.csdnHome" clearable></el-input>
        </el-form-item>
        <el-form-item label="开启Gitee访问" prop="Gitee">
          <el-switch v-model="Gitee"></el-switch>
        </el-form-item>
        <el-form-item label="Gitee 主页访问地址" prop="giteeHome" size="mini" v-if="Gitee">
          <el-input v-model="ruleForm.giteeHome" clearable></el-input>
        </el-form-item>
        <el-form-item label="开启知乎访问" prop="ZhiHu">
          <el-switch v-model="ZhiHu"></el-switch>
        </el-form-item>
        <el-form-item label="知乎 主页访问地址" prop="zhihuHome" size="mini" v-if="ZhiHu">
          <el-input v-model="ruleForm.zhihuHome" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            size="medium  "
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { SetList } from "../../units/api.js";
import { Update } from "../../units/api.js";
export default {
  data() {
    return {
      GitHub:true,
      Csdn:true,
      Gitee:true,
      ZhiHu:true,
      imageUrl: "",
      ruleForm: {

      },
      rules: {
        blogName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        author: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      let res = await SetList();
      console.log(res);
      this.ruleForm = res.data.data;
    },
    submitForm(formName) {
      
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await Update(this.ruleForm)
          console.log(res);
          this.getList();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
      this.ruleForm.avatar = URL.createObjectURL(file.raw);
      console.log(this.ruleForm.avatar );
      

      console.log("图片上传成功",res, file);
      
      
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
  
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style scoped>
.demo-ruleForm {
  text-align: start;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>